/*
 *
 *  * Copyright (c) 2016. For Intelligent Group.
 *
 */

package com.intelligent.ispc.core.functional.r;


import com.google.common.collect.Lists;
import com.intelligent.ispc.AbstractIntegrationTest;
import com.intelligent.ispc.core.dto.CPWarningRuleDto;
import com.intelligent.ispc.r.analysis.RCommandFactory;
import com.intelligent.ispc.r.analysis.spc.NDCommand;
import com.intelligent.ispc.r.analysis.spc.NDConfiguration;
import com.intelligent.ispc.r.dto.AnalysisParamDto;
import com.intelligent.ispc.r.dto.NormalDistributionDto;
import com.intelligent.ispc.r.utils.REnConnector;
import com.intelligent.ispc.r.utils.RUtils;
import com.intelligent.ispc.utils.ApplicationPathUtil;
import com.intelligent.ispc.utils.SystemConfig;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.test.annotation.Rollback;

import java.io.File;
import java.io.IOException;
import java.util.List;

import static org.mockito.BDDMockito.given;

public class RCommandT extends AbstractIntegrationTest {

    private SystemConfig systemConfig;

    private int digitNum = 2;
    private List<CPWarningRuleDto> cPWarningRuleList;

    @Before
    public void baseTest() {
        systemConfig = Mockito.mock(SystemConfig.class);

        given(4).willReturn(4);
        given(6).willReturn(6);

        digitNum = 6;

        cPWarningRuleList = Lists.newArrayList();
        String[] ruleName = new String[]{"CPU", "CPL", "CPK", "CP", "CA"};
        for (int i = 0; i < ruleName.length; i++) {
            CPWarningRuleDto ruleDto = new CPWarningRuleDto();
            if (!"CA".equals(ruleName[i])) {
                ruleDto.setAcceptableBoundaryValue("1.0");
                ruleDto.setExcellentBoundaryValue("1.67");
                ruleDto.setGoodBoundaryValue("1.33");
                ruleDto.setRectificationBoundaryValue("0.67");
            } else {
                ruleDto.setAcceptableBoundaryValue("25");
                ruleDto.setExcellentBoundaryValue("12.5");
                ruleDto.setGoodBoundaryValue("12.5");
                ruleDto.setRectificationBoundaryValue("50");
            }
            ruleDto.setIndiceName(ruleName[i]);
            cPWarningRuleList.add(ruleDto);
        }
    }

    @Test
    @Rollback(false)
    public void analyseNDData() {

        int digitNum = 4;
        NDCommand command = RCommandFactory.createNDCommand();


        AnalysisParamDto paramDto = new AnalysisParamDto();
        paramDto.setAnalysisData(new double[]{1069.194, 1064.382, 1051.104, 1059.608, 1062.426, 1059.368, 1060.233, 1058.030, 1052.559, 1064.650, 1057.772, 1054.524, 1050.515, 1055.019, 1053.053, 1053.406, 1051.764, 1047.792, 1056.025, 1056.325, 1057.515, 1051.781, 1056.874, 1048.341, 1053.336, 1053.115, 1054.639, 1058.697, 1044.581, 1052.302, 1052.034, 1051.510, 1050.629, 1050.036, 1049.159, 1050.256, 1074.276, 1066.295, 1066.968, 1059.653, 1064.109, 1055.838, 1059.379, 1060.439, 1061.518, 1014.447, 1012.553, 1012.167, 1012.420, 1011.992, 1012.256, 1012.631, 1013.079, 1012.878});
        paramDto.setGroupNum(6);
        paramDto.setUslValue(1000D);
        paramDto.setLslValue(1001D);

        NDConfiguration configuration = new NDConfiguration();
        configuration.setDigitNum(digitNum);
        configuration.setCPWarningRules(cPWarningRuleList);

        command.setConfiguration(configuration);
        command.setAnalysisParamDto(paramDto);

        command.execute();

        NormalDistributionDto dto = command.getResultDto();
        System.out.println(dto);
    }

    public static void main(String[] args) throws IOException {

        REnConnector con = RUtils.getInstance().getConnector();
        String version = con.getEval("R.version.string").asString();
        System.out.println(version);
//        System.out.println(System.getProperty("user.dir"));

        System.out.println(ApplicationPathUtil.getCanonicalPath());

        File directory = new File("");
//        System.out.println(directory.getCanonicalPath());
//        System.out.println(directory.getAbsolutePath());


        String dir = ApplicationPathUtil.getCanonicalPath() + "rscripts/analysis_NormalDistribution.R";
//         String dir = "J:/ISPC/desk/ispc_desktop3/ispc.d.service/target/test-classes/rscripts/analysis_NormalDistribution.R";
        System.out.println(dir);
        con.setInput("n", 2D);
        con.setInput("x", new double[]{1, 2, 3, 4, 5, 6});

        con.execEval("source(\"" + dir + "\")");
        double avg = con.getOutputDouble("avgValue");
        double min = con.getOutputDouble("minValue");

        System.out.println(avg);
        System.out.println(min);

        con.disconnect();

    }
}
